import { Model, DataTypes } from 'sequelize'
import { sequelize } from "../config/index.js"
import Post from './Post.js'
import Category from './Category.js'

// 定义一个 PostCategory 类 继承 Model
class PostCategory extends Model {}

// 通过 init 方法定义表的字段等信息
PostCategory.init(
  {
    postID: {
      type: DataTypes.INTEGER,
      references: {
        model: Post,
        key: 'id',
      },
    },
    categoryID: {
      type: DataTypes.INTEGER,
      references: {
        model: Category,
        key: 'id',
      },
    },
  },
  { sequelize, modelName: 'PostCategory', timestamps: false }
)

//设置 Category 表与 Post 表之间的关系 (多对多)
Post.belongsToMany(Category, { through: PostCategory, foreignKey: 'postID' })
Category.belongsToMany(Post, { through: PostCategory, foreignKey: 'categoryID' })

export default PostCategory